package pers.xhh.frontapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.xhh.frontapi.common.Mi;
import pers.xhh.frontapi.common.ResultCode;
import pers.xhh.frontapi.dao.MiGoodsDao;
import pers.xhh.frontapi.entity.MiAttributeEntity;
import pers.xhh.frontapi.entity.MiGoodsAttrEntity;
import pers.xhh.frontapi.entity.MiGoodsEntity;
import pers.xhh.frontapi.entity.MiGoodsPicsEntity;
import pers.xhh.frontapi.entity.to.goods.MiGoodsTo;
import pers.xhh.frontapi.entity.vo.goods.MiGoodsPutVo;
import pers.xhh.frontapi.entity.vo.goods.MiGoodsVo;
import pers.xhh.frontapi.service.MiAttributeService;
import pers.xhh.frontapi.service.MiGoodsAttrService;
import pers.xhh.frontapi.service.MiGoodsPicsService;
import pers.xhh.frontapi.service.MiGoodsService;
import pers.xhh.frontapi.util.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wei-xhh
 * @date 2020-11-22
 */
@Service
public class MiGoodsServiceImpl extends ServiceImpl<MiGoodsDao, MiGoodsEntity> implements MiGoodsService {

    @Autowired
    MiAttributeService miAttributeService;

    @Autowired
    MiGoodsAttrService miGoodsAttrService;

    @Autowired
    MiGoodsPicsService miGoodsPicsService;

    @Override
    public Mi listGoods(String query, Integer cateOneId, Integer pagenum, Integer pagesize) {
        if (pagenum == null || pagesize == null) {
            return Mi.error(ResultCode.ERROR303.getMessage(), ResultCode.ERROR303.getStatus());
        }
        if (pagenum <= 0 || pagesize <= 0) {
            return Mi.error(ResultCode.ERROR304.getMessage(), ResultCode.ERROR304.getStatus());
        }

        // ->查询
        Page<MiGoodsEntity> page = new Page<>(pagenum, pagesize);
        QueryWrapper<MiGoodsEntity> queryWrapper = new QueryWrapper<>();
        if (query != null) {
            queryWrapper = new QueryWrapper<MiGoodsEntity>()
                    .like("goods_name", query);
        }
        if(cateOneId != null){
            queryWrapper.eq("cat_one_id", cateOneId);
        }
        this.page(page, queryWrapper);

        // —>结果
        List<MiGoodsEntity> records = page.getRecords();

        if (records.size() > 0) {
            List<MiGoodsVo> collect = records.stream().map(item -> {
                MiGoodsVo miGoodsVo = new MiGoodsVo();
                miGoodsVo.setCat_id(item.getCatId());
                miGoodsVo.setCat_one_id(item.getCatOneId());
                miGoodsVo.setCat_two_id(item.getCatTwoId());
                miGoodsVo.setCat_three_id(item.getCatThreeId());
                miGoodsVo.setGoods_id(item.getGoodsId());
                miGoodsVo.setGoods_name(item.getGoodsName());
                miGoodsVo.setGoods_number(item.getGoodsNumber());
                miGoodsVo.setGoods_price(item.getGoodsPrice());
                miGoodsVo.setGoods_weight(item.getGoodsWeight());
                miGoodsVo.setGoods_state(item.getGoodsState());
                miGoodsVo.setIs_promote(item.getIsPromote() == 1);
                miGoodsVo.setAdd_time(DateUtils.formatDate(item.getCreateTime()));
                // 查询pics
                List<MiGoodsPicsEntity> miGoodsPicsEntityList =
                        miGoodsPicsService.list(new QueryWrapper<MiGoodsPicsEntity>().eq("goods_id", item.getGoodsId()));
                if(miGoodsPicsEntityList != null && miGoodsPicsEntityList.size() > 0){
                    List<MiGoodsTo.Pics> picsList = miGoodsPicsEntityList.stream().map(picsEntity -> {
                        MiGoodsTo.Pics pics = new MiGoodsTo.Pics();
                        pics.setPic(picsEntity.getPicsBig());
                        return pics;
                    }).collect(Collectors.toList());
                    miGoodsVo.setPics(picsList);
                }

                return miGoodsVo;
            }).collect(Collectors.toList());
            long total = page.getTotal();
            long current = page.getCurrent();
            Map<String, Object> map = new HashMap<>();
            map.put("total", total);
            map.put("pagenum", current);
            map.put("goods", collect);

            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), map);
        } else {
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "查询商品为空");
        }
    }

    @Override
    public Mi getGoodsById(Integer id) {
        if(id == null || id <= 0){
            return Mi.error(ResultCode.ERROR308.getMessage(), ResultCode.ERROR308.getStatus());
        }

        MiGoodsEntity byId = this.getById(id);
        if(byId != null){
            MiGoodsPutVo miGoodsVo = new MiGoodsPutVo();
            miGoodsVo.setCat_id(byId.getCatId());
            miGoodsVo.setCat_one_id(byId.getCatOneId());
            miGoodsVo.setCat_two_id(byId.getCatTwoId());
            miGoodsVo.setCat_three_id(byId.getCatThreeId());
            miGoodsVo.setGoods_id(byId.getGoodsId());
            miGoodsVo.setGoods_introduce(byId.getGoodsIntroduce());
            miGoodsVo.setGoods_name(byId.getGoodsName());
            miGoodsVo.setGoods_number(byId.getGoodsNumber());
            miGoodsVo.setGoods_price(byId.getGoodsPrice());
            miGoodsVo.setGoods_weight(byId.getGoodsWeight());
            miGoodsVo.setGoods_state(byId.getGoodsState());
            miGoodsVo.setIs_promote(byId.getIsPromote() == 1);
            miGoodsVo.setAdd_time(DateUtils.formatDate(byId.getCreateTime()));

            // 查询pics
            List<MiGoodsPicsEntity> miGoodsPicsEntityList =
                    miGoodsPicsService.list(new QueryWrapper<MiGoodsPicsEntity>().eq("goods_id", id));
            if(miGoodsPicsEntityList != null && miGoodsPicsEntityList.size() > 0){
                List<MiGoodsTo.Pics> collect = miGoodsPicsEntityList.stream().map(item -> {
                    MiGoodsTo.Pics pics = new MiGoodsTo.Pics();
                    pics.setPic(item.getPicsBig());
                    return pics;
                }).collect(Collectors.toList());
                miGoodsVo.setPics(collect);
            }

            // 查询attrs
            List<MiGoodsAttrEntity> miGoodsAttrEntityList =
                    miGoodsAttrService.list(new QueryWrapper<MiGoodsAttrEntity>().eq("goods_id", id));
            if(miGoodsAttrEntityList != null && miGoodsAttrEntityList.size() > 0){
                List<Map<String, Object>> collect = miGoodsAttrEntityList.stream().map(item -> {
                    MiAttributeEntity miAttributeEntity =
                            miAttributeService.getById(item.getAttrId());
                    Map<String, Object> map = new HashMap<>();
                    if(miAttributeEntity != null){
                        map.put("goods_id", id);
                        map.put("attr_id", item.getAttrId());
                        map.put("attr_value", item.getAttrValue());
                        map.put("attr_name", miAttributeEntity.getAttrName());
                        map.put("attr_sel", miAttributeEntity.getAttrSel());
                        map.put("attr_write", miAttributeEntity.getAttrWrite());
                        map.put("attr_vals", miAttributeEntity.getAttrVals());
                    }
                    return map;
                }).collect(Collectors.toList());
                miGoodsVo.setAttrs(collect);
            }
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), miGoodsVo);
        } else {
            return Mi.ok(ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getStatus(), "没有该商品");
        }
    }

}
